import { Component, OnInit, Input } from '@angular/core';
import * as moment from "moment";
import { DomSanitizer } from '@angular/platform-browser';
declare var $: any;
@Component({
  selector: 'app-product-detail-form',
  templateUrl: './product-detail-form.component.html',
  styleUrls: ['./product-detail-form.component.css']
})
export class ProductDetailFormComponent implements OnInit {

  @Input() modal = {
    id: "details",
    title: "XX管理 - 详情"
  }
  _data = {
    tree: [],
    terminal: [],
    form: [{
      label: "",
      value: ""
    }],
    description: "",
    imgUrl: ""
  }
  prop: string = "magazines" // 服务端返回的包含数据的属性
  @Input() set data(value) {
    let valueStatus = false;
    if (Object.prototype.toString.call(value) === "[object Object]") {
      if (
        (value as Object).hasOwnProperty(this.prop)
        // && (value as Object).hasOwnProperty("specialResourceDatasVoList")
        && (value as Object).hasOwnProperty("resources")
        && (value as Object).hasOwnProperty("resCategoryRelationsList")
      ) {
        valueStatus = true
      }
    }
    if (valueStatus&&value[this.prop]) {
      //console.log(value[this.prop])
      $("#description").empty();
      this._data.description = value[this.prop].description;
      
      if(this.prop == "wechatshares"){
        this._data.description = value[this.prop].htmltext;
      }
      $("#description").append(this._data.description)
      // this._data.description = this.sanitizer.bypassSecurityTrustHtml(this._data.description);
      this._data.terminal = value.specialResourceDatasVoList;
      // this._data.form = this.prdSrv.getFromStruct(this._type).map(item => {
      //   let _value = value[this.prop][item.key]
      //   //console.log(item,this._data.form)
      //   //_value.createtime =  moment(_value.createtime).format("YYYY-MM-DD")
      //   // if(item.label == '出版日期'){
      //   //   _value.value = moment(_value.value).format("YYYY-MM-DD")
      //   // }
      //   if (!_value) {
      //     _value = value.resources[item.key]
      //   }
      //   if(typeof _value == "boolean"){
      //     _value = _value?"是":"不是"
      //   }
      //   return {
      //     label: item.label,
      //     value: _value
      //   }
      // })
      this._data.imgUrl = value[this.prop].frontCover?value[this.prop].frontCover:value["resources"].thumbnail
      this._data.tree = (value.resCategoryRelationsList as Array<{ resCategoryId: string }>).map(item => item.resCategoryId)
    }
  }
  @Input() set type(type: string) {
    if (type == "periodical") {
      this.prop = "magazines"
    } else {
      this.prop = type
    }
    this._type = type
  }
  _type: string
  constructor(
    private sanitizer: DomSanitizer
  ) { }

  ngOnInit() {
    
  }
  getLeftData(data){
    let middle = (data.form.length+7)/2 - 7
    return data.form.slice(0,middle)
  }
  getRightData(data){
    let middle = (data.form.length+7)/2 - 7
    return data.form.slice(middle)
  }

}
